/** 
*编辑角色
*/
<template>
    <vp-dialog ref="editDialogRef" :span="1">
        <template #title>
            <vp-label v-if="type == 'add'" zh="新增" en="Add" align="left"/>
            <vp-label v-else zh="修改" en="Edit" align="left"/>
        </template>
        <el-form label-suffix=":" v-loading="loading"  :model="formData" :rules="formRules" ref="formRef" label-width="120px">
            <el-row>
                <vp-col :span="1">
                    <el-form-item :label="$t('role.roleCode')" prop="roleCode">
                        <template #label><vp-label zh="编码" en="Code"/></template>
                        <el-input v-model="formData.roleCode" v-trim maxlength="30"></el-input>
                    </el-form-item>
                </vp-col>
                <vp-col :span="1">
                    <el-form-item :label="$t('role.roleName')" prop="roleName">
                        <template #label><vp-label zh="名称" en="Name"/></template>
                        <el-input v-model="formData.roleName" v-trim maxlength="30"></el-input>
                    </el-form-item>
                </vp-col>
                <vp-col :span=1>
                    <el-form-item :label="$t('item.remark')" prop="roleRemark">
                        <template #label><vp-label zh="备注" en="Remark"/></template>
                        <el-input type="textarea" v-model="formData.roleRemark" v-trim maxlength="50"></el-input>
                    </el-form-item>
                </vp-col>
            </el-row>
        </el-form>
        <template #footer>
            <vp-btn-confirm :loading="saveLoading" @click="handleSave"/>
        </template>
    </vp-dialog>
</template>
<script setup lang="ts" name="editRole">
import { ref } from 'vue';
import request from '/@/utils/request';
import validateUtils from '/@/utils/validateUtils';

    const emits = defineEmits(['formSubmited']);   
    const formData = ref({
        id:"",
        roleCode:"",
        roleName:"",
        roleRemark:"",
    });

    const formRules = {
        roleCode:[ 
            validateUtils.notNull,
            validateUtils.notChinese
        ],
        roleName: [
            validateUtils.notNull
        ]
    }

    const loading = ref(false);
    const type = ref('add');
    const pageType = ref(1);//页面类型，1功能角色 2机型角色
    const saveLoading = ref(false);

    const editDialogRef = ref();
    const formRef = ref();
    const initForm = ()=>{
        formData.value = {
            id:"",
            roleCode:"",
            roleName:"",
            roleRemark:"",
        }
    }
    const handleSave = () => {
        if(loading.value){
            return;
        }
        saveLoading.value = true;
        formRef.value.validate((valid: Boolean)=>{
            if(valid){
                request.post(getRequestUrl()+type.value, {
                    ...formData.value
                }).then(()=>{
                    emits("formSubmited");
                    editDialogRef.value.handleClose();
                }).catch(()=>{
                }).finally(()=>{
                    saveLoading.value = false;
                })
            }else{
                saveLoading.value = false;
            }
        })
    }
    const handleOpen = (_pageType:number, _type: string, id: string)=>{
        type.value = _type;
        pageType.value = _pageType;
        initForm();
        editDialogRef.value.handleOpen();
        if(_type === 'edit'){
            loading.value = true;
            request.get(getRequestUrl()+`${id}`).then((res)=>{
                formData.value = {
                    ...formData.value,
                    ...res.data
                };
                loading.value = false;
            })
        }
    }

    const getRequestUrl = ()=> {
        let url = '/system/role/';
        if (pageType.value === 2) {
            url = '/system/modelRole/';
        }
        return url;
    }

    defineExpose({
        handleOpen,
    })

</script>
